﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tilver.Coder.Core;
using Tilver.Coder.Utility;

namespace Tilver.Coder.Win.Dialogs
{
    public partial class ColumnInterpretorDialog : Form
    {
        public List<CoderColumn> Columns { get; set; }
        private CoderDataBase _dataBase = null;
        private string _commandText = string.Empty;
        private CommandType _commandType = CommandType.Text;
        private List<CoderParameter> _paras = null;

        public ColumnInterpretorDialog()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="dataBase"></param>
        /// <param name="commandText"></param>
        /// <param name="commandType"></param>
        /// <param name="paras"></param>
        public void Set(CoderDataBase dataBase, string commandText, CommandType commandType, List<CoderParameter> paras)
        {
            _dataBase = dataBase;
            _commandText = commandText;
            _commandType = commandType;
            _paras = paras.Where(w => w.Direction == ParameterDirection.Input || w.Direction == ParameterDirection.InputOutput).ToList();

            foreach (var para in _paras)
            {
                var row = new DataGridViewRow();
                row.CreateCells(dgvParameters,
                    para.Name,
                    para.Description,
                    para.DbType,
                    para.Value == null ? true : false,
                    para.Value ?? null);
                row.Tag = para;
                dgvParameters.Rows.Add(row);
            }
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            Columns = new List<CoderColumn>();
            try
            {
                foreach (DataGridViewRow row in dgvParameters.Rows)
                {
                    var para = row.Tag as CoderParameter;
                    var allowNullCell = row.Cells[3] as DataGridViewCell;
                    var valueCell = row.Cells[1] as DataGridViewCell;

                    if ((bool)allowNullCell.Value)
                        para.Value = null;
                    else
                        para.Value = valueCell.Value == null ? null : valueCell.Value.ToString();
                }

                var ds = DBTextStyleUtil.GetDBTextStyle(_dataBase.DataProviderType).ExecuteSql(_dataBase.ConnectionString, _commandText, _commandType, _paras);
                var dt = ds.Tables[0];
                foreach (DataColumn dc in dt.Columns)
                {
                    var newColumn = new CoderColumn();
                    newColumn.Name = dc.ColumnName;
                    newColumn.CodeName = dc.ColumnName.ToCoderPropertyName();
                    newColumn.DbType = DBTypeUtil.ConvertToDbType(dc.DataType);
                    newColumn.AllowDBNull = dc.AllowDBNull;
                    newColumn.CSharpType = DBTypeUtil.ConvertToCSharpType(newColumn.DbType, newColumn.AllowDBNull);
                    newColumn.Size = dc.MaxLength;
                    Columns.Add(newColumn);
                }

                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
